﻿// <copyright file="MainUI.cs" company="Pelican Design &amp; Development">
//      Copyright (c) Pelican Design &amp; Development. All Rights Reserved.
// </copyright>

namespace AppDomains
{
    using System;
    using System.Collections.ObjectModel;
    using System.IO;
    using System.Security;

    /// <summary>
    /// Provides general user interface in a sandboxed context.
    /// </summary>
    public class MainUI : CommonMainUI
    {
        /// <summary>
        /// Collection of loaded plugins.
        /// </summary>
        private Collection<CommonPlugIn> plugIns;

        /// <summary>
        /// Initializes a new instance of the MainUI class.
        /// </summary>
        public MainUI()
        {
            this.plugIns = new Collection<CommonPlugIn>();
        }

        /// <summary>
        /// Tests permissions.
        /// </summary>
        public override void TestFilePermissions()
        {
            Console.WriteLine("1. Trying to write file through common tools library. Expecting success...");
            DataTools.WriteFileForMe("MainUI.txt", "Hello from main UI." + Environment.NewLine + "I'm saving this through DataTools, because it has more privileges than me.");

            Console.WriteLine();
            Console.WriteLine("2. Trying to write file outside the directory through common tools library. Expecting internal exception...");
            try
            {
                DataTools.WriteFileForMe(@"..\MainUI.txt", "Hello from main UI." + Environment.NewLine + "I'm saving this through DataTools, and I achieved to crack the library by writing a file outside the directory.");
                Console.WriteLine("\tWhat? There is a huge problem: the library was cracked.");
            }
            catch (SecurityException)
            {
                Console.WriteLine("\tOk. Access was denied, as expected.");
            }

            Console.WriteLine();
            Console.WriteLine("3. Trying to write file myself. Expecting exception...");
            try
            {
                File.WriteAllText(Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data"), "MainUIDirect.txt"), "Hello from main UI." + Environment.NewLine + "I'm saving this directly myself, and will fail to do it, because I don't have enough privileges.");
                Console.WriteLine("\tWhat? There is a huge problem: the sandboxed code had just accessed a file.");
            }
            catch (SecurityException ex)
            {
                if (ex.PermissionType.FullName == "System.Security.Permissions.FileIOPermission")
                {
                    Console.WriteLine("\tOk. It failed as expected. Great!");
                }
                else
                {
                    throw;
                }
            }

            Console.WriteLine();
        }

        /// <summary>
        /// Adds plugins to be shown in main window.
        /// </summary>
        /// <param name="plugIns">A collection of loaded plugins.</param>
        public override void UsePlugIns(Collection<CommonPlugIn> plugIns)
        {
            this.plugIns.Clear();
            foreach (var plugIn in plugIns)
            {
                this.plugIns.Add(plugIn);
            }
        }

        /// <summary>
        /// Displays the window.
        /// </summary>
        public override void ShowDialog()
        {
            Console.WriteLine("Creating window...");
            MainWindow mainWindow = new MainWindow();

            Console.WriteLine("Adding controls...");
            foreach (var plugIn in this.plugIns)
            {
                mainWindow.stackPanelPlugIns.Children.Add(plugIn.MainControl);
            }

            Console.WriteLine("Displaying window...");
            mainWindow.ShowDialog();
        }
    }
}
